import collections


class Solution(object):
    def minKBitFlips(self, nums, k):
        ans = 0
        st = collections.deque()
        for index, num in enumerate(nums):
            while st and st[0] <= index - k:
                st.popleft()
            if (num + len(st)) % 2 == 0:
                st.append(index)
                ans += 1
        while st and st[0] <= len(nums) - k:
            st.popleft()
        if not st:
            return ans
        else:
            return -1





data = Solution()
# nums = [0, 0, 0, 1, 0, 1, 1, 0]
# k = 3
# print(data.minKBitFlips(nums, k))
nums = [1, 1, 0]
k = 2
print(data.minKBitFlips(nums, k))